﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SalaryChannel.AdviseService;
using Silverlight;
using System.Text.RegularExpressions;

namespace SalaryChannel
{
    public partial class AdviseControl : UserControl
    {
        private int _pageSize = 10;

        public int PageSize
        {
            get { return _pageSize; }
            set { _pageSize = value; }
        }

        private int _pageIndex = 1;

        public int PageIndex
        {
            get { return _pageIndex; }
            set
            {
                if (this.AdviseGrid.ItemsSource == null && value!=1)
                {
                    return;
                }
                if (value < 1)
                {
                    return;
                }
                if (value > _pageIndex)
                {
                    if (this.AdviseGrid.ItemsSource != null
                        && (this.AdviseGrid.ItemsSource as AdviceEntity[]).Length < PageSize)
                    {
                        return;
                    }
                   
                }
                _pageIndex = value;
                this.PageIndexTB.Text = value.ToString();
                GoToPage();
            }
        }
        static Regex digitalRegex = new Regex(@"^\d+$");
        static Regex telRegex = new Regex(@"^1\d{10}$");
        public AdviseControl()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(AdviseContro_Loaded);
            AdviseGrid.SelectionChanged += new SelectionChangedEventHandler(AdviseGrid_SelectionChanged);

            ServiceInstance.AdviseClient.GetAdviseByUserIDCompleted += new EventHandler<GetAdviseByUserIDCompletedEventArgs>(AdviseClient_GetAdviseByUserIDCompleted);
            ServiceInstance.AdviseClient.GetAdviseByIDCompleted += new EventHandler<SalaryChannel.AdviseService.GetAdviseByIDCompletedEventArgs>(AdviseClient_GetAdviseByIDCompleted);
            ServiceInstance.AdviseClient.InsertAdviseCompleted += new EventHandler<InsertAdviseCompletedEventArgs>(AdviseClient_InsertAdviseCompleted);

            this.PageIndexTB.TextChanged += new TextChangedEventHandler(PageIndexTB_TextChanged);
        }

        void PageIndexTB_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.PageIndexTB.Text == Properties.Resource.One) return;
            if (!digitalRegex.IsMatch(this.PageIndexTB.Text))
            {
                this.PageIndexTB.Text = Properties.Resource.One;
            }
        }

        void AdviseClient_GetAdviseByUserIDCompleted(object sender, GetAdviseByUserIDCompletedEventArgs e)
        {
            try
            {
                this.AdviseGrid.ItemsSource = e.Result;
            }
            catch
            {
                App.MessageBox.ShowDialog(Properties.Resource.QueryAdvicesFailed, Title.Error);
               // MessageBox.Show("查询建议失败，" + ex.Message, "异常", MessageBoxButton.OK);
            }
        }

        void AdviseClient_InsertAdviseCompleted(object sender, InsertAdviseCompletedEventArgs e)
        {
            string msg=string.Empty;
            try
            {
                
                 if (e.Result)
                {
                    msg = Properties.Resource.SubmitAdviceSucessfully;
                    this.ReadOnly = true;
                }
                else
                {
                    msg = Properties.Resource.TryAgain; 
                }
            }
            catch
            {

                msg = Properties.Resource.TryAgain; 
            }
            App.MessageBox.ShowDialog(msg, Title.Information);
        }

        public bool ReadOnly
        {
            set
            {
                this.TelTB.IsReadOnly = value;
                this.TitleTB.IsReadOnly = value;
                this.BodyTB.IsReadOnly = value;
                //this.NewBtn.IsEnabled = value;
                //this.SubmitBtn.IsEnabled = !value;

                this.NewBtn.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
                this.SubmitBtn.Visibility = value ? Visibility.Collapsed : Visibility.Visible;
            }
        }
        void AdviseClient_GetAdviseByIDCompleted(object sender, GetAdviseByIDCompletedEventArgs e)
        {
            try
            {
                this.TitleTB.Text = e.Result.Title;
                this.TelTB.Text = e.Result.Tel;
                this.BodyTB.Text = e.Result.Body;
                this.CreateTimeTB.Text = e.Result.CreateTime.ToShortDateString() + " " + e.Result.CreateTime.ToShortTimeString();
                this.CreaterTB.Text = string.Format("[{0}]{1}", e.Result.CreateID, e.Result.CreaterName);
                this.ReadOnly = true;
            }
            catch 
            {
                App.MessageBox.ShowDialog(Properties.Resource.QueryAdvicesFailed, Title.Error);
            }
        }

        void AdviseGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (AdviseGrid.SelectedItem != null)
                {
                    ServiceInstance.AdviseClient.GetAdviseByIDAsync((AdviseGrid.SelectedItem as AdviceEntity).ID);
                }
            }
            catch 
            {
                App.MessageBox.ShowDialog(Properties.Resource.ConnectServerFailed, Title.Error);
                
            }
        }
        public void ClearUI()
        {
            if (this.Visibility == Visibility.Collapsed)
            {
                return;
            }
            if (!App.IsAuth(13))
            {
                this.SearchIDTB.Text = App.CurrentUser.UserID.ToString();
                this.SearchIDTB.IsReadOnly = true;
            }
            else
            {
                this.SearchIDTB.IsReadOnly = false;
                this.SearchIDTB.Text = string.Empty;
            }

            this.AdviseGrid.ItemsSource = null;
          
            InitialUI();
        }
        void InitialUI()
        {
            this.ReadOnly = false;
            this.TitleTB.Text = string.Empty;
            this.TelTB.Text = string.Empty;
            this.CreateTimeTB.Text = DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString();
            this.CreaterTB.Text = string.Format("[{0}]{1}", App.CurrentUser.UserID,
            App.CurrentUser.UserName);
            this.BodyTB.Text = string.Empty;
        }
        void AdviseContro_Loaded(object sender, RoutedEventArgs e)
        {

            this.ReadOnly = true;
        }

        private void SubmitBtn_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(this.TitleTB.Text)
                || string.IsNullOrEmpty(this.TelTB.Text)
                || string.IsNullOrEmpty(BodyTB.Text))
            {
                App.MessageBox.ShowDialog(Properties.Resource.InputInforWarning, Title.Warning);
               
                return;
            }
            if (!telRegex.IsMatch(this.TelTB.Text))
            {
                App.MessageBox.ShowDialog(Properties.Resource.IllegalTel, Title.Warning);
                
                return;
            }
            try
            {
                ServiceInstance.AdviseClient.InsertAdviseAsync(App.CurrentUser.UserID,
                        this.TitleTB.Text.Trim(), this.BodyTB.Text.Trim(), this.TelTB.Text);
               
            }
            catch
            {

                App.MessageBox.ShowDialog(Properties.Resource.ConnectServerFailed, Title.Error);
            }
                
        }

        private void NewBtn_Click(object sender, RoutedEventArgs e)
        {
            InitialUI();

        }

        void GoToPage()
        {
            try
            {
                ServiceInstance.AdviseClient.GetAdviseByUserIDAsync(this.SearchIDTB.Text,
                        this.PageSize, _pageIndex);
            }
            catch 
            {

                App.MessageBox.ShowDialog(Properties.Resource.ConnectServerFailed, Title.Error);
            }
        }
       
        private void SearchAdviseBtn_Click(object sender, MouseButtonEventArgs e)
        {
            this.PageIndex = 1;
            
        }

        private void GoToPageBtn_Click(object sender, RoutedEventArgs e)
        {
            this.PageIndex = int.Parse(this.PageIndexTB.Text);
           
        }

        private void NextBtn_Click(object sender, MouseButtonEventArgs e)
        {
            this.PageIndex++;
            
        }

        private void PreBtn_Click(object sender, MouseButtonEventArgs e)
        {
            this.PageIndex--;
        }
    }
}
